﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using Research.Model;
using Research.BLL;

namespace Research.Web
{
    /// <summary>
    /// MyWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class MyWebService : System.Web.Services.WebService
    {
        readonly BasicBll _basicBll=new BasicBll();

        [WebMethod]
        public string DrawChart(int rPrjID)
        {
            var charsModels = new List<HcharsModel>();

            var planScheduleList = _basicBll.GetPlanScheduleList(rPrjID);
            foreach (var planSchedule in planScheduleList)
            {
                var chartData = new List<object[]>();
                chartData.Add(new object[2] { DateParse(planSchedule.PlanBegDate), 0 });
                chartData.Add(new object[2] { DateParse(planSchedule.PlanEndDate), 100 });
                charsModels.Add(new HcharsModel { name = planSchedule.ScheduleName + "(计划)", data = chartData });

                planSchedule.RprjID = rPrjID;
                var actualScheduleList = _basicBll.GetActualScheduleList(planSchedule);
                if (actualScheduleList.Count<=0)
                    continue;

                var chartDataActual = new List<object[]>();
                chartDataActual.Add(new object[2] { DateParse(planSchedule.PlanBegDate), 0 });
                foreach (var actualSchedule in actualScheduleList)
                {
                    chartDataActual.Add(new object[2]
                                            {DateParse(actualSchedule.RegDate), actualSchedule.Percentage});
                }
                charsModels.Add(new HcharsModel { name = planSchedule.ScheduleName + "(实际)", data = chartDataActual });

            }
            return JsonConvert.SerializeObject(charsModels, new DataTableConverter());
        }

        public long DateParse(DateTime? str)
        {
           var thisDate = str.GetValueOrDefault();
            var timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳  
            return (thisDate.Ticks - timeStamp.Ticks) / 10000; //注意这里有时区问题，用now就要减掉8个小时
        }
    }
}
